route.ts

route.ts

基本信息

  • 类型: API 路由
  • 路径: ./src/app/api/prompts/[id]/versions/route.ts
  • 路由: /api/prompts/[id]/versions

概述

Prompt 版本管理接口。支持获取版本历史和创建新版本。

HTTP 方法

  • GET: 获取版本列表
  • POST: 创建新版本

路径参数

参数类型必填说明
idstringPrompt ID

GET - 获取版本列表

响应

成功 (200)

[
  {
    "id": "version_id",
    "promptId": "prompt_id",
    "version": 3,
    "content": "版本内容",
    "changeNote": "更新说明",
    "createdAt": "2024-01-01T00:00:00Z",
    "createdBy": "user_id",
    "author": {
      "name": "作者名",
      "username": "username"
    }
  }
]

错误 (500)

{
  "error": "server_error",
  "message": "Something went wrong"
}

POST - 创建新版本

Body 参数

参数类型必填说明
contentstring新版本内容
changeNotestring变更说明,最多 500 字符

响应

成功 (201)

返回创建的版本对象

错误

未授权 (401)
{
  "error": "unauthorized",
  "message": "You must be logged in"
}
无权限 (403)
{
  "error": "forbidden",
  "message": "You can only add versions to your own prompts"
}
未找到 (404)
{
  "error": "not_found",
  "message": "Prompt not found"
}
验证失败 (400)
{
  "error": "validation_error",
  "message": "Invalid input"
}
内容未改变 (400)
{
  "error": "no_change",
  "message": "Content is the same as current version"
}

依赖

  • next/server - Next.js 服务器组件
  • zod - 输入验证
  • @/lib/auth - 认证
  • @/lib/db - Prisma 数据库客户端

权限

  • GET: 公开访问
  • POST: 仅 Prompt 作者

注意事项

  1. 创建新版本会同时更新 Prompt 的当前内容
  2. 版本号自动递增
  3. 内容必须与当前版本不同才能创建新版本
  4. 使用数据库事务确保版本创建和内容更新的一致性
← 返回目录